package com.cn.netty.customize.handler;

import com.cn.netty.basic.EchoServer;
import com.cn.netty.customize.Codec;
import com.cn.netty.customize.BusinessMeassage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.nio.charset.StandardCharsets;

/**
 * 编码
 * Message实体转化成字节
 */
public class EncodeHandler extends MessageToByteEncoder<BusinessMeassage> {

  @Override
  protected void encode(ChannelHandlerContext ctx, BusinessMeassage msg, ByteBuf out) throws Exception {
    byte[] encode = Codec.encode(msg);
    ByteBuf byteBuf = Unpooled.copiedBuffer(encode);
    out.writeBytes(byteBuf);//传输时 需要用ByteBuf
  }

}
